package com.per.mybootall.socket.workDoing;

import javax.net.ssl.SSLServerSocket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;

import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;

public class TcpServer {
//    public static void main(String[] args) throws IOException {
//
//        try{
//        //建立套接字
//        ServerSocket server=new ServerSocket(4700);
//
//        //监听
//        Socket socket=server.accept();
//            System.out.println("有一个链接进来了==》》》》");
//
//        //建立连接【从socket读取】
//        InputStreamReader Socin=new InputStreamReader(socket.getInputStream());
//        BufferedReader SocBuf=new BufferedReader(Socin);
//
//
//        //向socket中写
//        PrintWriter SocOut=new PrintWriter(socket.getOutputStream());
//
//        System.out.println("server接收的Client:"+SocBuf.readLine());
//
//            //发送数据到客户端
//            SocOut.println("6666666666666666666666666");
//            SocOut.flush();
//            System.out.println("server即将发送的消息为:"+"6666666666666666666666666666666");
//
//        //关闭socker和io
//        socket.close();
//        Socin.close();
//        SocOut.close();
//        } catch (Exception e) {
//            System.out.println("error:"+e);
//        }
//    }

    public static void main(String[] args) {
                 try {
                         startSSLServer();
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
            }

    public static void startSSLServer() throws IOException, java.security.cert.CertificateException {
                // 监听端口
                int port = 8089;
                // 密钥库文件
                 String keyFile = "D:\\JAVA\\bin\\shfqkeystore.jks";
                // 密钥库的密码
                String keyFilePass = "123456";
                // 密钥别名的密码
                 String keyPass = "123456";
                // 安全连接套接字
                SSLServerSocket sslsocket = null;
                // 密钥库
                 KeyStore ks;
                 // 密钥管理工厂
                 KeyManagerFactory kmf;
                // 安全连接方式
                 SSLContext sslc = null;
                 // 初始化安全连接的密钥
                 try {
                         ks = KeyStore.getInstance("JKS");
                         ks.load(new FileInputStream(keyFile), keyFilePass.toCharArray());
                         kmf = KeyManagerFactory.getInstance("SunX509");
                         kmf.init(ks, keyPass.toCharArray());
                        // sslc = SSLContext.getInstance("SSLv3");
                         sslc = SSLContext.getInstance("TLSv1.2");

                         sslc.init(kmf.getKeyManagers(), null, null);
                     } catch (Exception e) {
                         e.printStackTrace();

                     }
                 // 用安全连接的工厂来创建安全连接套接字
                 SSLServerSocketFactory sslssf = sslc.getServerSocketFactory();
                // 创建并进入监听
                 sslsocket = (SSLServerSocket) sslssf.createServerSocket(port);
                 System.out.println("服务器进入监听状态===》》》");
                 while (true) {
                        // 接受客户端的连接
                         SSLSocket ssocket = (SSLSocket) sslsocket.accept();
                         System.out.println("服务器成功链接到了==》》》");
                         // 以下代码同socket通讯实例中的代码
                         BufferedReader socketIn = new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
                         DataOutputStream socketOut = new DataOutputStream(ssocket.getOutputStream());
                         String s = null;

                         s = socketIn.readLine();
                         System.out.println("客户端发送的信息为: " + s);


                         s = "接收成功";
                         String a=new String(s.getBytes("utf-8"),"utf-8");
                         System.out.print("服务器返回的信息为： " + s);
                         socketOut.write(a.getBytes());
                         ssocket.close();
                     }

             }
}
